/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "sle_ssap_server_stub.h"
#include "sle_error_code.h"
#include "sle_ssap_server_callback_stub.h"
#include "g_val.h"
#include "mock_ipcobject_stub.h"
#include "sle_ssap_server_proxy.h"

class SleSsapServerStubTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    virtual void SetUp() override;
    virtual void TearDown() override;

};
void SleSsapServerStubTest::SetUp()
{
}

void SleSsapServerStubTest::TearDown()
{
}

void SleSsapServerStubTest::SetUpTestCase()
{
}

void SleSsapServerStubTest::TearDownTestCase()
{
}

namespace OHOS {
namespace NearLink {
namespace SleStandard {

class SleSsapServerMock : public SleSsapServerStub {
public:
    SleErrorCode RegisterSsapServer(const Uuid &uuid, uint8_t &serverId, const sptr<ISleSsapServerCallback> &callback){
            (void )uuid;
            serverId = 1;
            return SLE_NO_ERROR;
    }
     SleErrorCode UnregisterSsapServer(uint8_t serverId, const sptr<ISleSsapServerCallback> &callback){
        serverId = 2;
        return SLE_NO_ERROR;
    }
     SleErrorCode AddService(uint8_t serverId, const Uuid &uuid, bool isPrimary, uint16_t &handle){
        (void )uuid;
        (void)serverId;
        (void)isPrimary;
        handle = 1;
        return SLE_NO_ERROR;
    }
     SleErrorCode AddProperty(
        uint8_t serverId, uint16_t serviceHandle, const SsapServerPropertyParam &param, uint16_t &handle){
            (void )serverId;
            handle = 1;
            return SLE_NO_ERROR;
    }
     SleErrorCode AddDescriptor(
        uint8_t serverId, uint16_t serviceHandle, uint16_t propHandle, const SsapServerDescriptorParam &param){
            return SLE_NO_ERROR;
    }
     SleErrorCode RemoveAllServices(uint8_t serverId){
        return SLE_NO_ERROR;
    }
     SleErrorCode StartService(uint8_t serverId, uint16_t serviceHandle){
        return SLE_NO_ERROR;
    }
     SleErrorCode SendResponse(uint8_t serverId, uint16_t connectId, const SsapServerSendRspParam &param){
        return SLE_NO_ERROR;
    }
     SleErrorCode NotifyIndicate(uint8_t serverId, uint16_t connectId, const SsapServerNotifyParam &param){
        return SLE_NO_ERROR;
    }
     SleErrorCode NotifyIndicateByUuid(
        uint8_t serverId, uint16_t connectId, const SsapServerNotyfyByUuidParam &param){
            return SLE_NO_ERROR;
    }
    SleErrorCode SetServerMtuInfo(uint8_t serverId, uint32_t mtu, uint16_t version){
        return SLE_NO_ERROR;
    }
};

class SleSsapServerCallbackStubMock : public SleSsapServerCallbackStub {
public:
    void OnStartService(uint8_t serverId, uint16_t handle, uint32_t errCode)
    {

    }
    void OnAllServiceDelete(uint8_t serverId, uint32_t errCode)
    {

    }
    void OnReadRequest(
    uint8_t serverId, uint16_t connectId, const SsapServerReqReadCbParam &param, uint32_t errCode)
    {

    }
    void OnWriteRequest(
    uint8_t serverId, uint16_t connectId, const SsapServerReqWriteCbParam &param, uint32_t errCode)
    {

    }
    void OnMtuInfoChanged(
    uint8_t serverId, uint16_t connectId, uint32_t mtu, uint16_t version, uint32_t errCode)
    {

    }
};


/************************************************kaihong/communication/nearlink/sle/sle_standard/frameworks/ipc/source stub******************************************************** */
HWTEST_F(SleSsapServerStubTest, OnStartServiceInner_401, testing::ext::TestSize.Level0)
{
    SleSsapServerCallbackStubMock ssapClient;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerCallbackStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    data.WriteUint32(1);
    EXPECT_EQ(ssapClient.OnRemoteRequest(SLE_SSPA_SERVER_CALLBACK_START_SERVICE, data, reply, option), NO_ERROR);
}


HWTEST_F(SleSsapServerStubTest, OnAllServiceDeleteInner_401, testing::ext::TestSize.Level0)
{
    SleSsapServerCallbackStubMock ssapClient;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerCallbackStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint32(1);
    EXPECT_EQ(ssapClient.OnRemoteRequest(SLE_SSPA_SERVER_CALLBACK_DELETE_ALL_SERVICES, data, reply, option), NO_ERROR);
}

HWTEST_F(SleSsapServerStubTest, OnReadRequestInner_401, testing::ext::TestSize.Level0)
{
    SleSsapServerCallbackStubMock ssapClient;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerCallbackStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    SsapServerReqReadCbParam param;
    data.WriteParcelable(&param);
    data.WriteUint32(1);

    EXPECT_EQ(ssapClient.OnRemoteRequest(SLE_SSPA_SERVER_CALLBACK_READ_REQUEST, data, reply, option), NO_ERROR);
}

HWTEST_F(SleSsapServerStubTest, OnWriteRequestInner_401, testing::ext::TestSize.Level0)
{
    SleSsapServerCallbackStubMock ssapClient;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerCallbackStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    SsapServerReqWriteCbParam param;
    data.WriteParcelable(&param);
    data.WriteUint32(1);

    EXPECT_EQ(ssapClient.OnRemoteRequest(SLE_SSPA_SERVER_CALLBACK_WRITE_REQUEST, data, reply, option), NO_ERROR);
}

HWTEST_F(SleSsapServerStubTest, OnMtuInfoChangedInner_401, testing::ext::TestSize.Level0)
{
    SleSsapServerCallbackStubMock ssapClient;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerCallbackStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    data.WriteUint32(1);
    data.WriteUint16(1);
    data.WriteUint32(1);
    EXPECT_EQ(ssapClient.OnRemoteRequest(SLE_SSPA_SERVER_CALLBACK_MTU_INFO_CHANGED, data, reply, option), NO_ERROR);
}




/************************************************kaihong/communication/nearlink/sle/sle_standard/frameworks/ipc/source proxy******************************************************** */

HWTEST_F(SleSsapServerStubTest, UnregisterSsapServer_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1;
    sptr<SleSsapServerCallbackStubMock> callback = new SleSsapServerCallbackStubMock();
    SleErrorCode result = proxy->UnregisterSsapServer( serverId, callback);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, UnregisterSsapServer_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1;
    sptr<SleSsapServerCallbackStubMock> callback = new SleSsapServerCallbackStubMock();
    SleErrorCode result = proxy->UnregisterSsapServer( serverId, callback);
    EXPECT_EQ(result, (SleErrorCode)0);
}



HWTEST_F(SleSsapServerStubTest, AddService_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1;
    Uuid uuid;
    bool isPrimary = true;
    uint16_t handle = 0;
    SleErrorCode result = proxy->AddService( serverId, uuid,  isPrimary, handle);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, AddService_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1;
    Uuid uuid;
    bool isPrimary = true;
    uint16_t handle = 0;
    SleErrorCode result = proxy->AddService( serverId, uuid,  isPrimary, handle);
    EXPECT_EQ(result, (SleErrorCode)0);
}

HWTEST_F(SleSsapServerStubTest, AddProperty_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1; uint16_t serviceHandle = 0; SsapServerPropertyParam param; uint16_t handle = 1;
    SleErrorCode result = proxy->AddProperty( serverId,  serviceHandle,   param,  handle);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, AddProperty_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1; uint16_t serviceHandle = 0; SsapServerPropertyParam param; uint16_t handle = 1;
    SleErrorCode result = proxy->AddProperty( serverId,  serviceHandle,   param,  handle);
    EXPECT_EQ(result, (SleErrorCode)0);
}


HWTEST_F(SleSsapServerStubTest, AddDescriptor_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1; uint16_t serviceHandle = 1; uint16_t propHandle = 2; SsapServerDescriptorParam param;
    SleErrorCode result = proxy->AddDescriptor( serverId,  serviceHandle,  propHandle, param);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, AddDescriptor_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1; uint16_t serviceHandle = 1; uint16_t propHandle = 2; SsapServerDescriptorParam param;
    SleErrorCode result = proxy->AddDescriptor( serverId,  serviceHandle,  propHandle, param);
    EXPECT_EQ(result, (SleErrorCode)0);
}


HWTEST_F(SleSsapServerStubTest, SendResponse_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerSendRspParam param;
    SleErrorCode result = proxy->SendResponse( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, SendResponse_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerSendRspParam param;
    SleErrorCode result = proxy->SendResponse( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)0);
}


HWTEST_F(SleSsapServerStubTest, NotifyIndicate_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerNotifyParam  param;
    SleErrorCode result = proxy->NotifyIndicate( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, NotifyIndicate_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerNotifyParam param;
    SleErrorCode result = proxy->NotifyIndicate( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)0);
}



HWTEST_F(SleSsapServerStubTest, NotifyIndicateByUuid_301, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 1;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerNotyfyByUuidParam param;
    SleErrorCode result = proxy->NotifyIndicateByUuid( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)7);
}

HWTEST_F(SleSsapServerStubTest, NotifyIndicateByUuid_302, testing::ext::TestSize.Level0)
{
    sptr<IRemoteObject> mockStub = new MockIPCObjectStub(u"test");
    std::shared_ptr<SleSsapServerProxy> proxy = std::make_shared<SleSsapServerProxy>(mockStub);
    g_ControlVal = 0;

    uint8_t serverId = 1; uint16_t connectId = 1; SsapServerNotyfyByUuidParam param;
    SleErrorCode result = proxy->NotifyIndicateByUuid( serverId,  connectId,  param);
    EXPECT_EQ(result, (SleErrorCode)0);
}
















/************************************************communication/nearlink/sle/sle_standard/services/ipc******************************************************** */

HWTEST_F(SleSsapServerStubTest, RegisterSsapServerInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    std::shared_ptr<Uuid> uuid = std::make_shared<Uuid>();
    uuid->ConvertToSleUuid();
    sptr<IRemoteObject> remoteObject = new SleSsapServerCallbackStubMock();
    data.WriteParcelable(uuid.get());
    data.WriteRemoteObject(remoteObject);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_REGISTER_SERVER, data, reply, option), NO_ERROR);
}
/******************************************************************************************************************************** */

HWTEST_F(SleSsapServerStubTest, UnregisterSsapServerInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    sptr<IRemoteObject> remoteObject = new SleSsapServerCallbackStubMock();
    data.WriteUint8(1);
    data.WriteRemoteObject(remoteObject);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_UNREGISTER_SERVER, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */
HWTEST_F(SleSsapServerStubTest, AddService_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    std::shared_ptr<Uuid> uuid = std::make_shared<Uuid>();
    uuid->ConvertToSleUuid();

    data.WriteParcelable(uuid.get());
    data.WriteBool(true);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_ADD_SERVICE, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, AddPropertyInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);

    SsapServerPropertyParam propertyParam;
    data.WriteParcelable(&propertyParam);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_ADD_PROPERTY, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, AddDescriptorInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    data.WriteUint16(1);

    SsapServerDescriptorParam propertyParam;
    data.WriteParcelable(&propertyParam);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_ADD_DESCRIPTOR, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, RemoveAllServicesInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);

    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_REMOVE_ALL_SERVICES, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, StartServiceInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);
    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_START_SERVICE, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, SendResponseInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);

    SsapServerPropertyParam propertyParam;
    data.WriteParcelable(&propertyParam);
    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_SEND_RESPONSE, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, NotifyIndicateInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);

    SsapServerPropertyParam propertyParam;
    data.WriteParcelable(&propertyParam);
    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_NOTIFY_INDICATE, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */


HWTEST_F(SleSsapServerStubTest, NotifyIndicateByUuidInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint16(1);

    SsapServerNotyfyByUuidParam propertyParam;
    data.WriteParcelable(&propertyParam);
    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_NOTIFY_INDICATE_BY_UUID, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */

HWTEST_F(SleSsapServerStubTest, SetServerMtuInfoInner_001, testing::ext::TestSize.Level0)
{
    SleSsapServerMock ssapServer;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;
    data.WriteInterfaceToken(SleSsapServerStub::GetDescriptor());

    data.WriteUint8(1);
    data.WriteUint32(10);
    data.WriteUint16(1);
    EXPECT_EQ(ssapServer.OnRemoteRequest(SLE_SSAP_SERVER_SET_MTU_INFO, data, reply, option), NO_ERROR);
}

/******************************************************************************************************************************** */













}}}